package cn.gcyl.module.biz.service.specialmeeting;

import cn.gcyl.module.biz.dal.dataobject.SysMemberUserDO;
import cn.gcyl.module.biz.dal.dataobject.platforminformation.PlatformInformationDO;
import cn.gcyl.module.biz.dal.dataobject.useractionlog.UserActionLogDO;
import cn.gcyl.module.biz.dal.mysql.SysMemberUserMapper;
import cn.gcyl.module.biz.dal.mysql.useractionlog.UserActionLogMapper;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.gcyl.module.biz.controller.admin.specialmeeting.vo.*;
import cn.gcyl.module.biz.dal.dataobject.specialmeeting.SpecialMeetingDO;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.util.object.BeanUtils;

import cn.gcyl.module.biz.dal.mysql.specialmeeting.SpecialMeetingMapper;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.convertList;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.diffList;
import static cn.gcyl.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.SPECIAL_MEETING_NOT_EXISTS;

/**
 * 专场会议管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SpecialMeetingServiceImpl implements SpecialMeetingService {

    @Resource
    private SpecialMeetingMapper specialMeetingMapper;
    @Resource
    private UserActionLogMapper userActionLogMapper;
    @Resource
    private SysMemberUserMapper sysMemberUserMapper;


    @Override
    public Long createSpecialMeeting(SpecialMeetingSaveReqVO createReqVO) {
        // 插入
        SpecialMeetingDO specialMeeting = BeanUtils.toBean(createReqVO, SpecialMeetingDO.class);
        if (specialMeeting.getIsTop()==1){
            //把表中所有的置为0
            SpecialMeetingDO specialMeetingDO = specialMeetingMapper.selectOne(
                    new LambdaQueryWrapper<SpecialMeetingDO>().eq(SpecialMeetingDO::getIsTop, 1)
            );
            if (ObjectUtil.isNotEmpty(specialMeetingDO)){
                specialMeetingDO.setIsTop(0);
                specialMeetingMapper.updateById(specialMeetingDO);
            }
        }
        specialMeetingMapper.insert(specialMeeting);

        // 返回
        return specialMeeting.getId();
    }

    @Override
    public void updateSpecialMeeting(SpecialMeetingSaveReqVO updateReqVO) {
        // 校验存在
        validateSpecialMeetingExists(updateReqVO.getId());
        // 更新
        SpecialMeetingDO updateObj = BeanUtils.toBean(updateReqVO, SpecialMeetingDO.class);
        if (updateObj.getIsTop()==1){
            //把表中所有的置为0
            SpecialMeetingDO specialMeetingDO = specialMeetingMapper.selectOne(
                    new LambdaQueryWrapper<SpecialMeetingDO>().eq(SpecialMeetingDO::getIsTop, 1)
            );
            if (ObjectUtil.isNotEmpty(specialMeetingDO)){
                specialMeetingDO.setIsTop(0);
                specialMeetingMapper.updateById(specialMeetingDO);
            }
        }
        specialMeetingMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpecialMeeting(Long id) {
        // 校验存在
        validateSpecialMeetingExists(id);
        // 删除
        specialMeetingMapper.deleteById(id);
    }

    @Override
        public void deleteSpecialMeetingListByIds(List<Long> ids) {
        // 删除
        specialMeetingMapper.deleteByIds(ids);
        }


    private void validateSpecialMeetingExists(Long id) {
        if (specialMeetingMapper.selectById(id) == null) {
            throw exception(SPECIAL_MEETING_NOT_EXISTS);
        }
    }

    @Override
    public SpecialMeetingDO getSpecialMeeting(Long id) {
        return specialMeetingMapper.selectById(id);
    }
    @Override
    public SpecialMeetingDO getAppSpecialMeeting(Long id) {
        //访问一次接口浏览量加1
        SpecialMeetingDO specialMeetingDO = specialMeetingMapper.selectById(id);
        specialMeetingDO.setVisitors(specialMeetingDO.getVisitors()+1);
        specialMeetingMapper.updateById(specialMeetingDO);
        Long loginUserId = getLoginUserId();
        //添加操作日志
        if(loginUserId != null) {
            UserActionLogDO userActionLogDO = new UserActionLogDO();
            userActionLogDO.setUserId(loginUserId);
            userActionLogDO.setAction("访问专场会议:" + specialMeetingDO.getMeetingName());
            userActionLogMapper.insert(userActionLogDO);
        }
        return specialMeetingDO;
    }

    @Override
    public PageResult<SpecialMeetingDO> getSpecialMeetingPage(SpecialMeetingPageReqVO pageReqVO) {
        return specialMeetingMapper.selectPage(pageReqVO);
    }
    @Override
    public PageResult<SpecialMeetingDO> getAppSpecialMeetingPage(AppSpecialMeetingPageReqVO pageReqVO) {
        return specialMeetingMapper.getAppSpecialMeetingPage(pageReqVO);
    }

}